import 'dart:convert';

class XORCryptoUtil {
  static const String keyStr = '7faf65872ac9691a1f3bcce83e79bc51';
  static int k = keyStr.length;

  /// 异或加密
  static String _coding(String message) {
    int len = message.length;
    String str = '';
    for (int i = 0; i < len; i++) {
      int msgCode = message.codeUnitAt(i);
      int keyCode = keyStr.codeUnitAt(i % k);
      var res = msgCode ^ keyCode;
      str += String.fromCharCode(res);
    }
    return str;
  }

  /// 加密
  static String encoding(String plaintext) {
    return enBase64(_coding(enBase64(plaintext)));
  }

  /// 解密
  static String decoding(String cipherText) {
    return deBase64(_coding(deBase64(cipherText)));
  }

  static String enBase64(String content) {
    var bytes = utf8.encode(content);
    return base64Encode(bytes);
  }

  static String deBase64(String content) {
    return utf8.decode(base64Decode(content));
  }
}

void main() {
  String name = "!!<>清?:{}(风)}1}ab明cd1234《月》？：！";
  String eres = XORCryptoUtil.encoding(name);
  String dres = XORCryptoUtil.decoding(eres);

  // String dres3 = XORCryptoUtil.deBase64("VWhFT0FYOTJja05SVXdCUWYzMWVCbmdJZlZNNlVTdFVVRlo2VUM0aVdsWitKU3NNVkFkcVczc0lJ\r\nZzkvZlhnV2ZDZERXeW9pV0FVPQ==");
  String dres2 = XORCryptoUtil.decoding(
      "UhEOAX92ckNRUwBQf31eBngIfVM6UStUUFZ6UC4iWlZ+JSsMVAdqW3sIIg9/fXgWfCdDWyoiWAU=");
  print('加密后：$eres');
  print('解密后：$dres');
  print('解密后：$dres2');
  // print('解密后：$dres3');
}
